﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{

	import flash.events.MouseEvent;
	
	public class CanvasArrowHandler extends CanvasDefaultHandler {
		private var mouseX:Number;
		private var mouseY:Number;
		private var drawingArrow:Boolean;
		
		/**
		 Constructor
		*/
		function CanvasArrowHandler(c:CanvasCore){
			super(c);
			mouseX = 0;
			mouseY = 0;
			drawingArrow = false;
		}
		
		override public function activate(param = null):void{
			if(! activated){
				activated = true;
				core.showBlockPanel(true);
				core.getBlockPanel().useHandCursor = false;
				core.removeSelections();
				if(param != null) shapeToCreate = param;
			}
		}
		
		override public function deactivate():void{
			if(activated){
				activated = false;
				if(drawingArrow){
					core.getBlockPanel().removeEventListener(MouseEvent.MOUSE_MOVE, drawShapeArrowShadow);
					core.getBlockPanel().graphics.clear();
					drawingArrow = false;
				}
				core.getBlockPanel().useHandCursor = true;
			}
		}
		
		override public function clickFront(evt:MouseEvent):void{
			if(! drawingArrow){
				drawingArrow = true;
				mouseX = evt.localX;
				mouseY = evt.localY;
				core.getBlockPanel().addEventListener(MouseEvent.MOUSE_MOVE, drawShapeArrowShadow);
			}else{
				drawingArrow = false;
				core.getBlockPanel().removeEventListener(MouseEvent.MOUSE_MOVE, drawShapeArrowShadow);
				core.getBlockPanel().graphics.clear();
				this.addArrowShape(mouseX, mouseY, evt.localX, evt.localY);
				mouseX = 0;
				mouseY = 0;
			}
		}
		
		// -------------- PRIVATE FUNCTIONS ----------------//
		/**
		  Draws an arrow under the mouse when inserting an arrow,
		  arrow grows from first click to current mouse position
		*/
		private function drawShapeArrowShadow(evt:MouseEvent):void{
			core.getBlockPanel().graphics.clear();
			core.getBlockPanel().graphics.lineStyle(2, 0xAA3333);
			
			core.getBlockPanel().graphics.moveTo(mouseX, mouseY);
			core.getBlockPanel().graphics.lineTo(evt.stageX, evt.stageY);
		}
		
		/**
		  Adds an arrow shape inside the specified vectors oriented correctly and
		  scales it to the correct size
		  @param x1 Number X of vector 1
		  @param y1 Number Y of vector 1
		  @param x2 Number X of vector 2
		  @param y2 Number Y of vector 2
		*/
		private function addArrowShape(x1:Number, y1:Number, x2:Number, y2:Number):void{
			var shap:ShapeObject = core.addShapeObjXY((x1 + x2)/2, (y1 + y2)/2, shapeToCreate);
								// Distance of two points / width of the arrow
			var scalex:Number = Math.max(Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)), 20)/shap.width;
			var scaley:Number = 1;
			
			if(shap.width*scalex < 80){ // If arrow is shorter than 80px
				scaley = (shap.width*scalex)/80;
			}
			
			core.scaleShape(shap, scalex, scaley, true, false); // Save action and it is not the first action in the cluster
			core.rotateShapeAtan2(shap, y2 - y1, x2 - x1, true, false); // Save action and it is not the first action in the cluster
		}
	}
}